#!/bin/sh

clean(){
	FILES="configure configure.ac depcomp install-sh missing mkinstalldirs aclocal.m4 autom4te.cache"
	FILES="$FILES Makefile.in doc/Makefile.in man/Makefile.in script/Makefile.in src/Makefile.in"
	FILES="$FILES Makefile doc/Makefile man/Makefile script/Makefile src/Makefile"
	FILES="$FILES config.*"
	FILES="$FILES stamp-h1 autoscan.log"	
	echo "clean $FILES"
	rm -rf $FILES
}


escape_slashes () {
    sed 's/\//\\\//g' 
}

change_line (){
    local OLD_LINE_PATTERN="$1"
    local NEW_LINE="$2"
    local FILE="$3"

    local NEW=$(echo "${NEW_LINE}" | escape_slashes)
    sed -i .bak '/'"${OLD_LINE_PATTERN}"'/s/.*/'"${NEW}"'/' "${FILE}"
    mv "${FILE}.bak" /tmp/
}
test_sed(){

new_db_host="DB_HOSTNAME=good replaced with 122.334.567.90"
echo "DB_HOSTNAME=xxx 122.334.567.90" >  /tmp/test4sed
echo "DB_HOSTNAME=xxx 122.334.567.90 xx" >>  /tmp/test4sed
sed -i "/DB_HOST/c $new_db_host" "/tmp/test4sed"
#cat  /tmp/test4sed
}

setup_a(){
	autoscan
	mv configure.scan configure.ac
}

make_configure(){
	aclocal
	autoconf
	autoheader
	automake --add-missing
}
cmd=$1

if [ "$cmd" = "a" ]; then
	setup_a
	#test_sed
	sed  -i '/AC_INIT/c AC_INIT(lester, 0.4, lester@aa.com)' "configure.ac"
	autoconf
elif [ "$cmd" = "b" ]; then
	sed -i '/AC_INIT/a AM_INIT_AUTOMAKE(lester, 0.4)' "configure.ac"
	
	#echo "AM_INIT_AUTOMAKE(lester, 0.4)" >> "configure.ac"
	echo "regenernate configure"
	make_configure
elif [ "$cmd" = "c" ]; then
	sed -i '/Makefile/d' "configure.ac"
	sed -i '/AC_OUTPUT/c AC_OUTPUT(Makefile doc/Makefile man/Makefile script/Makefile src/Makefile)' "configure.ac"
	./configure
elif [ "$cmd" = "d" ]; then
	echo "genernate objectfile"
	make
elif [ "$cmd" = "clean" ]; then
	echo "clean files"
	clean
elif [ "$cmd" = "all" ]; then
# if you run shell 
# ./clean.sh clean; ./clean.sh a; ./clean.sh b; ./clean.sh c; ./clean.sh d
# in script need to run the below 
 ./clean.sh clean; 
 ./clean.sh a; 
 ./clean.sh b; 
 ./clean.sh c; 
 ./clean.sh d
fi

